<template>
  <div class="app-container">
    <div class="top-show">
      <el-form :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="80px">
        <el-row>

          <el-form-item label="支付状态"
            prop="payStatus">
            <el-select v-model="queryParams.payStatus"
              placeholder="请选择支付状态"
              clearable
              size="small"
              style="width: 232px">
              <el-option v-for="dict in payStatusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue" />
            </el-select>
          </el-form-item>
          <el-form-item label="支付时间"
            prop="payTimeArray">
            <el-date-picker v-model="queryParams.payTimeArray"
              type="daterange"
              size="small"
              align="right"
              :clearable="false"
              style="width: 232px"
              value-format="yyyy-MM-dd"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="客户名称"
            prop="customerName">
            <el-input v-model="queryParams.customerName"
              placeholder="请输入客户名称"
              clearable
              size="small"
              style="width: 232px"
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item style="margin-left: 10px">
            <el-button type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh"
              size="mini"
              @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>

    <el-row class="row-mbinfo">

      <el-col :span="1.5">

        <el-button type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport">导出</el-button>

      </el-col>
      <right-toolbar :showSearch.sync="showSearch"
        @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="divTable">
      <el-table border
        v-loading="loading"
        :data="rechargeList"
        @selection-change="handleSelectionChange">

        <el-table-column label="充值单号"
          align="center"
          prop="rechargeNo"
          min-width="130"
          :show-overflow-tooltip="true" />
        <el-table-column label="充值项目"
          align="center"
          prop="rechargeName"
          min-width="150" />
        <el-table-column label="客户名称"
          align="center"
          prop="customerName"
          :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column label="客户手机"
          align="center"
          prop="phone"
          :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column label="充值金额"
          align="center"
          prop="orderAmountStr"
          :show-overflow-tooltip="true" />
        <el-table-column label="兑换VIP"
            align="center"
            prop="levelName"
            :show-overflow-tooltip="true" />
        <el-table-column label="兑换积分"
            align="center"
            prop="score"
            :show-overflow-tooltip="true" />
        <el-table-column label="支付金额"
          align="center"
          prop="payAmountStr"
          :show-overflow-tooltip="true" />
        <el-table-column label="订单状态"
          align="center"
          prop="orderStatus"
          width="120"
          :formatter="orderStatusFormat"
          :show-overflow-tooltip="true" />
        <el-table-column label="支付状态"
          align="center"
          prop="payStatus"
          :formatter="payStatusFormat" />
          <!--
        <el-table-column label="下单时间"
          align="center"
          prop="createTime"
          width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        -->
        <el-table-column label="支付时间"
          align="center"
          prop="payTime"
          width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注"
          align="center"
          prop="remark">
        </el-table-column>
        <!-- <el-table-column label="未支付订单过期时间" align="center" prop="expireDate" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.expireDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column> -->

        <!-- <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" /> -->
        <!-- <el-table-column label="ip地址" align="center" prop="ip" :show-overflow-tooltip="true" /> -->
        <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-zoom-in" @click="handleShowDetail(scope.row)"
              v-hasPermi="['order:recharge:query']">查看</el-button>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['order:recharge:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['order:recharge:remove']">删除</el-button>
          </template>
        </el-table-column> -->
      </el-table>

      <pagination v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改充值订单对话框 -->
    <el-dialog :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      @keyup.enter.native="submitForm">
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="100px">
        <el-form-item label="平台ID"
          prop="platformUid">
          <el-input v-model="form.platformUid"
            placeholder="请输入平台ID" />
        </el-form-item>
        <el-form-item label="充值单号"
          prop="rechargeNo">
          <el-input v-model="form.rechargeNo"
            placeholder="请输入充值单号" />
        </el-form-item>
        <el-form-item label="客户ID"
          prop="customerUid">
          <el-input v-model="form.customerUid"
            placeholder="请输入客户ID" />
        </el-form-item>
        <el-form-item label="客户名称"
          prop="customerName">
          <el-input v-model="form.customerName"
            placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="手机号码"
          prop="phone">
          <el-input v-model="form.phone"
            placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="订单状态"
          prop="orderStatus">
          <el-select v-model="form.orderStatus"
            placeholder="请选择订单状态">
            <el-option v-for="dict in orderStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付状态"
          prop="payStatus">
          <el-select v-model="form.payStatus"
            placeholder="请选择支付状态">
            <el-option v-for="dict in payStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付方式"
          prop="payMethod">
          <el-select v-model="form.payMethod"
            placeholder="请选择支付方式">
            <el-option v-for="dict in payMethodOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单来源"
          prop="platSource">
          <el-select v-model="form.platSource"
            placeholder="请选择订单来源">
            <el-option v-for="dict in platSourceOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单金额"
          prop="orderAmount">
          <el-input v-model="form.orderAmount"
            placeholder="请输入订单金额" />
        </el-form-item>
        <el-form-item label="支付金额"
          prop="payAmount">
          <el-input v-model="form.payAmount"
            placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="货币单位"
          prop="currencyUnit">
          <el-select v-model="form.currencyUnit"
            placeholder="请选择货币单位">
            <el-option v-for="dict in currencyUnitOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付时间"
          prop="payTime">
          <el-date-picker clearable
            size="small"
            v-model="form.payTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单取消时间"
          prop="cancelTime">
          <el-date-picker clearable
            size="small"
            v-model="form.cancelTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择订单取消时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="买家留言"
          prop="buyMes">
          <el-input v-model="form.buyMes"
            placeholder="请输入买家留言" />
        </el-form-item>
        <el-form-item label="未支付订单过期时间"
          prop="expireDate">
          <el-date-picker clearable
            size="small"
            v-model="form.expireDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择未支付订单过期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注"
          prop="remark">
          <el-input v-model="form.remark"
            placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="ip地址"
          prop="ip">
          <el-input v-model="form.ip"
            placeholder="请输入ip地址" />
        </el-form-item>
      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看充值订单对话框 -->
    <el-dialog :title="viewTitle"
      :visible.sync="viewOpen"
      width="700px"
      append-to-body>
      <el-form ref="detailForm"
        label-width="100px">
        <el-row>
          <el-col :span="12"
            v-for="(item, index) in showData"
            :key="index"
            style="margin-top:-15px">
            <el-form-item :label="`${item.label}：`">
              <span>{{ item.value }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          size="mini"
          @click="handCloseDetail">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRecharge,
  getRecharge,
  delRecharge,
  addRecharge,
  updateRecharge,
  storeGroup,
  rechargeInfoTotal
} from "@/api/order/recharge";
import {
  selectGroup
} from "@/api/platform/store"
export default {
  name: "Recharge",
  components: {},
  data () {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick (picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      saveStoreInFoOptions: '',
      storeInFoOptions: [],
      storeGroupList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值订单表格数据
      rechargeList: [],
      // 弹出层标题
      title: "",
      //查看详细标题
      viewTitle: "",
      // 是否显示弹出层
      open: false,
      //是否显示查看详细弹出层
      viewOpen: false,
      //查看详细数据
      showData: [],
      // 订单状态字典
      orderStatusOptions: [],
      // 支付状态字典
      payStatusOptions: [],
      // 支付方式字典
      payMethodOptions: [],
      // 订单来源字典
      platSourceOptions: [],
      // 货币单位字典
      currencyUnitOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rechargeNo: null,
        customerName: null,
        phone: null,
        orderStatus: null,
        payStatus: null,
        payMethod: null,
        platSource: null,
        orderAmount: null,
        payAmount: null,
        currencyUnit: null,
        payTime: null,
        createTime: null,
        payTimeArray: [],
        cancelTime: null,
        buyMes: null,
        expireDate: null,
        remark: null,
        ip: null,
        storeIds: [],
        storeGroupId: null
      },
      payMethods: [],
      columns: [{
        label: '订单ID',
        dataIndex: 'rechargeUid',
      },
      {
        label: '平台ID',
        dataIndex: 'platformUid',
      },
      {
        label: '充值单号',
        dataIndex: 'rechargeNo',
      },
      {
        label: '客户ID',
        dataIndex: 'customerUid',
      },
      {
        label: '客户名称',
        dataIndex: 'customerName',
      },
      {
        label: '手机号码',
        dataIndex: 'phone',
      },
      {
        label: '订单状态',
        dataIndex: 'orderStatus',
      },
      {
        label: '支付状态',
        dataIndex: 'payStatus',
      },
      {
        label: '支付方式',
        dataIndex: 'payMethod',
      },
      {
        label: '订单来源',
        dataIndex: 'platSource',
      },
      {
        label: '订单金额',
        dataIndex: 'orderAmount',
      },
      {
        label: '支付金额',
        dataIndex: 'payAmount',
      },
      {
        label: '货币单位',
        dataIndex: 'currencyUnit',
      },
      {
        label: '支付时间',
        dataIndex: 'payTime',
      },
      {
        label: '下单时间',
        dataIndex: 'createTime',
      },
      {
        label: '订单取消时间',
        dataIndex: 'cancelTime',
      },
      {
        label: '买家留言',
        dataIndex: 'buyMes',
      },
      {
        label: '未支付订单过期时间',
        dataIndex: 'expireDate',
      },
      {
        label: '备注',
        dataIndex: 'remark',
      },
      {
        label: 'ip地址',
        dataIndex: 'ip',
      },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        rechargeNo: [{
          required: true,
          message: "充值单号不能为空",
          trigger: "blur"
        }],
      },
      orderTotalData: 0
    };
  },
  watch: {
    ['queryParams.storeGroupId'] (val) {
      this.queryParams.storeIds = []
      if (val) {
        let storeArr = [], storeList = JSON.parse(this.saveStoreInFoOptions)
        this.storeGroupList.forEach(item => {
          if (item.groupId == val && item.storeIds) {
            storeArr = item.storeIds.split(',')
          }
        })

        let list = []
        storeList.forEach(item => {
          if (item.storeUid) {
            let sId = item.storeUid + ""
            if (storeArr.indexOf(sId) > -1) {
              list.push(item)
            }
          }
        })
        this.storeInFoOptions = list
      } else {
        this.storeInFoOptions = JSON.parse(this.saveStoreInFoOptions)
      }

    }
  },
  created () {
    this.queryParams.payTimeArray = [this.parseTime(new Date().getTime() - 6 * 24 * 60 * 60 * 1000, '{y}-{m}-{d}'), this.parseTime(new Date().getTime(), '{y}-{m}-{d}')]

    selectGroup().then(response => {
      this.storeInFoOptions = response.data || [];
      this.saveStoreInFoOptions = JSON.stringify(response.data || [])
    });
    storeGroup().then(response => {
      this.storeGroupList = response.data || [];
    });
    this.getList();
    this.getDicts("order_order_status").then(response => {
      this.orderStatusOptions = response.data;
    });
    this.getDicts("order_pay_status").then(response => {
      this.payStatusOptions = response.data;
    });
    this.getDicts("order_pay_method").then(response => {
      this.payMethodOptions = response.data;
    });
    this.getDicts("order_plat_source").then(response => {
      this.platSourceOptions = response.data;
    });
    this.getDicts("order_currency_unit").then(response => {
      this.currencyUnitOptions = response.data;
    });
    this.getRechargeInfoTotal()
  },
  methods: {
    getRechargeInfoTotal () {
      rechargeInfoTotal(this.queryParams).then((res) => {
        this.orderTotalData = res.msg
      })
    },
    toJumpCustomer (uid) {
      this.$router.push("/customer/detail/" + uid);
    },
    /** 查询充值订单列表 */
    getList () {
      this.loading = true;
      listRecharge(this.queryParams).then(response => {
        this.rechargeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 订单状态字典翻译
    orderStatusFormat (row, column) {
      return this.selectDictLabel(this.orderStatusOptions, row.orderStatus);
    },
    // 支付状态字典翻译
    payStatusFormat (row, column) {
      return this.selectDictLabel(this.payStatusOptions, row.payStatus);
    },
    // 支付方式字典翻译
    payMethodFormat (row, column) {
      return this.selectDictLabel(this.payMethodOptions, row.payMethod);
    },
    // 订单来源字典翻译
    platSourceFormat (row, column) {
      return this.selectDictLabel(this.platSourceOptions, row.platSource);
    },
    // 货币单位字典翻译
    currencyUnitFormat (row, column) {
      return this.selectDictLabel(this.currencyUnitOptions, row.currencyUnit);
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        rechargeUid: null,
        platformUid: null,
        rechargeNo: null,
        customerUid: null,
        customerName: null,
        phone: null,
        orderStatus: null,
        payStatus: null,
        payMethod: null,
        platSource: null,
        orderAmount: null,
        payAmount: null,
        currencyUnit: null,
        payTime: null,
        createTime: null,
        cancelTime: null,
        buyMes: null,
        expireDate: null,
        remark: null,
        storeIds: [],
        storeGroupId: null,
        ip: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      if (this.payMethods.length > 0) {
        this.queryParams.payMethod = this.payMethods.join(',')
      } else {
        this.queryParams.payMethod = null
      }

      this.queryParams.pageNum = 1;
      this.getList();
      this.getRechargeInfoTotal()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.payMethods = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.rechargeUid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加充值订单";
    },
    /** 查看详情按钮操作 **/
    handleShowDetail (row) {
      this.viewOpen = true;
      this.viewTitle = "查看充值订单";
      this.showData = [];
      this.columns.forEach(item => {
        this.showData.push({
          label: item.label,
          value: row[item.dataIndex]
        })
      })
    },
    handCloseDetail () {
      this.viewOpen = false
      this.showData = []
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const rechargeUid = row.rechargeUid || this.ids
      getRecharge(rechargeUid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充值订单";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rechargeUid != null) {
            updateRecharge(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecharge(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const rechargeUids = row.rechargeUid || this.ids;
      this.$confirm('是否确认删除充值订单编号为"' + rechargeUids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delRecharge(rechargeUids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('/prod-api/order/recharge/export', {
        ...this.queryParams
      }, `储值订单.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
.top-show {
  display: flex;
  background-color: #fff;
  border-radius: 6px;
  padding: 10px 10px 20px;
  .show-data {
    flex: 1;
    margin-left: 30px;
    margin-top: 10px;
    border: 1px solid #eee;
    height: 200px;
    max-width: 400px;
    .item-data {
      height: 100px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      padding: 0 30px;
      &:not(:last-child) {
        border-bottom: 1px solid #eee;
      }
      .item-data-show {
        font-size: 30px;
        color: #333;
        line-height: 1;
        margin-bottom: 10px;
      }
      .item-data-desc {
        font-size: 14px;
        color: #999;
      }
    }
  }
}
</style>
